<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索 -->
      <el-input v-model="query.value" clearable placeholder="输入商户代码/名称搜索" style="width: 200px;" class="filter-item"
                @keyup.enter.native="toQuery"/>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="toQuery">搜索</el-button>
      <!-- 新增 -->
      <div style="display: inline-block;margin: 0px 2px;">
        <el-button
          v-permission="['admin','courseType:add']"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="add">新增
        </el-button>
      </div>
    </div>
    <!--表单组件-->
    <eForm ref="form" :is-add="isAdd"/>

    <!--表格渲染-->
    <el-table border v-loading="loading" border :data="data" default-expand-all row-key="id" size="small">
      <el-table-column label="分类名称" prop="name"/>
      <el-table-column label="分类代码" prop="id">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="点我复制" placement="top-start">
            <span style="cursor: pointer" v-copy:callback="handleCopied" v-copy="scope.row.id">{{scope.row.id}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建日期">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        v-if="checkPermission(['admin','courseType:edit','courseType:del'])"
        label="操作" width="130px" align="center"
        fixed="right">
        <template slot-scope="scope">
          <el-button
            v-permission="['admin','courseType:edit']"
            size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
              </el-button>
            </div>
            <el-button
              v-permission="['admin','courseType:del']"
              slot="reference" :disabled="scope.row.id === 1" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  import initData from '@/mixins/initData'
  import {del} from '@/api/business/courseTypeApi'
  import {parseTime} from '@/utils/index'
  import eForm from './form'

  export default {
    components: {eForm},
    mixins: [initData],
    data() {
      return {
        delLoading: false, expand: true
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
      })
    },
    methods: {
      parseTime,
      beforeInit() {
        this.url = 'api/courseType'
        this.params = {page: this.page, size: this.size}
        const query = this.query
        const value = query.value
        if (value) {
          this.params['name'] = value
        }
        return true
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.delLoading = false
          this.$refs[id].doClose()
          this.init()
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
        }).catch(err => {
          this.delLoading = false
          this.$refs[id].doClose()
        })
      },
      add() {
        this.isAdd = true
        const _this = this.$refs.form
        _this.getCourseType()
        _this.form = {
          id: '',
          name: '',
          pid: '',
        }
        _this.dialog = true
      },
      changeExpand() {
        this.expand = !this.expand
      },
      edit(data) {
        this.isAdd = false
        const _this = this.$refs.form
        _this.getCourseType()
        _this.form = {
          id: data.id,
          name: data.name,
          pid: data.pid,
        }
        _this.dialog = true
      },
    }
  }
</script>

<style scoped>

</style>
